package com.iwown.sample4GApi.calculation;

import com.google.protobuf.InvalidProtocolBufferException;
import com.iwown.sample4GApi.protobuf.HisDataOuterClass;
import com.iwown.sample4GApi.protobuf.HisSpo2Data;
import com.iwown.sample4GApi.utils.MyDateTimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class Spo2Preprocessor {
    private static final Logger log = LoggerFactory.getLogger(Spo2Preprocessor.class);

    public void DataPrepare(byte[] payload) {
        HisDataOuterClass.HisNotification hisNotify = null;
        try {
            hisNotify = HisDataOuterClass.HisNotification.parseFrom(payload);
        } catch (InvalidProtocolBufferException ex) {
            ex.printStackTrace();
            return;
        }
        if(hisNotify.hasHisData()){
            HisDataOuterClass.HisData hisData = hisNotify.getHisData();
            if(hisData.hasSpo2()){
                HisSpo2Data.HisDataSpo2 spo2Data = hisData.getSpo2();
                /*
                    not like ecg, spo2 is not one time measure, but
                    continuous value, you can calculate af result
                    of random time range, combine all spo2 value of
                    the time range for calculation
                 */
                String data_time = MyDateTimeUtils.convert2DateTimeString(spo2Data.getTimeStamp().getDateTime().getSeconds());
                List<Integer> spo2List = spo2Data.getSpo2DataList();
                log.info(String.format("spo2 history time: %s",data_time));
                for(int i=0;i<spo2List.size();i++){
                    int value = spo2List.get(i);
                    int spo2 = (value >> 24) & 0xFF;
                    log.info(String.format("spo2: %d", spo2));
                }
            }
        }
    }
}
